package org.example.quartz.service.impl;

import org.example.quartz.service.UtilService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service("utilServiceImplSlave1")
public class UtilServiceImpl implements UtilService {


    @Value(value = "${spring.datasource.oracleBip}")
    private String oracleBIP;
    @Value(value = "${spring.datasource.GSYY}")
    private String GSYY;
    @Value(value = "${spring.datasource.yearValue}")
    private String YEARVALUE;
    @Value(value = "${spring.datasource.oracleBipBack}")
    private String oracleBIPback;

    public String getOracle() {
        return oracleBIP;
    }

    public String getGSYY() {
        return GSYY;
    }

    public String getYEAR() {
        return YEARVALUE;
    }

    /**
     * 获取当前年和上一年的年份
     *
     * @return
     */
    @Override
    public List<Integer> getYEARS() {
        List<Integer> years = new ArrayList<>();
        //查询脚本
        int YEARConfig = Integer.parseInt(YEARVALUE);
        //获取当前年份
        int YEAR = LocalDateTime.now().getYear();
        //如果当前年和配置文件的年份不相等，当前年份减一，添加到及合中
        if (YEAR > YEARConfig) {
            int NEWYEAR = YEAR - 1;
            years.add(NEWYEAR);
        }
        years.add(YEAR);
        return years;
    }

    public String getOracleback() {
        return oracleBIPback;
    }

    public String getTs(String methodName, JdbcTemplate jdbcTemplate) {

        String ts = "2024-01-01 00:00:00";
        //查询sql
        //String tsSql = "select DATE_FORMAT(yyts, '%Y-%m-%d %H:%i:%s') AS yyts from gs_yy_log where methodName = '"+methodName+"' order by yyts desc limit 1  ";
        String tsSql = "select to_char(yyts, 'yyyy-mm-dd hh24:mi:ss') as yyts from ( select * from " + GSYY + ".GS_YY_LOG WHERE METHODNAME = '" + methodName + "'  order by yyts DESC ) c where rownum= 1";
        List<Map<String, Object>> maps = jdbcTemplate.queryForList(tsSql);
        if (maps.size() > 0) {
            ts = maps.get(0).get("yyts").toString();
            if (ts == null || ts.equals("")) {
                if (methodName.equals("GLQuartz") || methodName.equals("GLTOMIDQuartz")) {
                    ts = "2025-01-01 00:00:00";
                } else {
                    ts = "1900-01-01 00:00:00";
                }
            }
        } else {
            if (methodName.equals("GLQuartz") || methodName.equals("GLTOMIDQuartz")) {
                ts = "2025-01-01 00:00:00";
            } else {
                ts = "1900-01-01 00:00:00";
            }
        }
        return ts;
    }

    /**
     * 根据中间库的org
     * 确定查询范围
     */
    @Override
    public String getGSOrgStr(JdbcTemplate jdbcTemplate) {
        String orgStr = "";
        //String tsSql = "SELECT GROUP_CONCAT(yyCode SEPARATOR '&') as orgStr FROM yy_org_orgs where gsCode is not null and gsCode != ''";
        String tsSql = "select listagg(yyCode,'&') within group (order by id) as orgStr from " + GSYY + ".yy_org_orgs where gsCode is not null ";
        List<Map<String, Object>> maps = jdbcTemplate.queryForList(tsSql);
        if (maps.size() > 0) {
            orgStr = (String) maps.get(0).get("orgStr");
            //替换，  变成 ','
            orgStr = "'" + orgStr + "'";
            orgStr = orgStr.replace("&", "','");
        } else {
            orgStr = "";
        }
        return orgStr;

    }

    @Override
    public String getItemTs(String methodName, String itemClass, JdbcTemplate jdbcTemplate) {
        String ts = "";
        //查询sql
        String tsSql = "select yyts from " + GSYY + ".gs_yy_log where methodName = '" + methodName + "'and itemClass = '" + itemClass + "' order by yyts desc limit 1  ";
        List<Map<String, Object>> maps = jdbcTemplate.queryForList(tsSql);
        if (maps.size() > 0) {
            ts = (String) maps.get(0).get("yyts");
            if (ts == null || ts.equals("")) {
                ts = "2024-01-01 00:00:00";
            }
        } else {
            ts = "2024-01-01 00:00:00";
        }
        return ts;
    }

    @Override
    public String getHZ() {
        String hz = "";
        if (ObjectUtils.isEmpty(oracleBIPback)) {
            hz = " ";
        } else {
            hz = "@" + oracleBIPback + " ";
        }
        return hz;
    }

}
